package cn.znzz.cjm.pet.service.impl;


import cn.znzz.cjm.basic.service.impl.BaseServiceImpl;
import cn.znzz.cjm.basic.util.CodeGenerateUtils;
import cn.znzz.cjm.basic.util.DistanceUtil;
import cn.znzz.cjm.basic.util.PageList;
import cn.znzz.cjm.basic.util.Point;
import cn.znzz.cjm.order.domain.PetAcquisitionOrder;
import cn.znzz.cjm.order.mapper.PetAcquisitionOrderMapper;
import cn.znzz.cjm.org.domain.Employee;
import cn.znzz.cjm.org.domain.Shop;
import cn.znzz.cjm.org.mapper.EmployeeMapper;
import cn.znzz.cjm.org.mapper.ShopMapper;
import cn.znzz.cjm.pet.domain.Pet;
import cn.znzz.cjm.pet.domain.SearchMasterMsg;
import cn.znzz.cjm.pet.mapper.SearchMasterMsgMapper;
import cn.znzz.cjm.pet.query.SearchMasterMsgQuery;
import cn.znzz.cjm.pet.service.IPetService;
import cn.znzz.cjm.pet.service.ISearchMasterMsgService;
import cn.znzz.cjm.product.domain.Product;
import cn.znzz.cjm.user.domain.Logininfo;
import cn.znzz.cjm.user.domain.User;
import cn.znzz.cjm.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Logininfo logininfo) {

        //获取当前登录人
        User user=userMapper.loadByLogininfoId(logininfo.getId());
        searchMasterMsg.setUser_id(user.getId());
        //寻找最近位置
        Point userPoint = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(userPoint, shopMapper.loadAll());
        searchMasterMsg.setShop_id(nearestShop.getId());
        searchMasterMsgMapper.save(searchMasterMsg);
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo logininfo) {
        Employee employee=employeeMapper.loadByLoginInfoId(logininfo.getId());
        query.setShopId(employee.getShop_id());
        query.setState(0);
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo logininfo) {
        Employee employee=employeeMapper.loadByLoginInfoId(logininfo.getId());
        query.setShopId(employee.getShop_id());
        query.setState(1);
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery query, Logininfo logininfo) {
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        return super.queryPage(query);
    }

    @Override
    public void handle(Pet pet, Logininfo currentLogin) {
        searchMasterMsgMapper.upadteState2ProccedById(pet.getSearch_master_msg_id());
        petService.add(pet);
        //生成收购订单
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.loadByLoginInfoId(currentLogin.getId());

        PetAcquisitionOrder order = pet2order(pet,searchMasterMsg,employee.getId());
        petAcquisitionOrderMapper.save(order);
    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg searchMasterMsg,Long employeeId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(searchMasterMsg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(searchMasterMsg.getUser_id());
        order.setOrderSn(orderSn);//订单编号
        order.setPet_id(pet.getId());
        order.setUser_id(searchMasterMsg.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(employeeId);
        return order;
    }
}
